var logger = require("pomelo-logger").getLogger(__filename);

module.exports = function (app) {
    return new Remote(app);
};

var Remote = function (app) {
    this.app = app;
};


Remote.prototype.getPlayerInfoByUid = function (msg, callback) {
    logger.info("get playerInfo msg = " + JSON.stringify(msg));
    if (!msg.hasOwnProperty("uid")) {
        callback({
            resultCode: 404,
            message: "uid为空"
        });
        return;
    }
    var uid = parseInt(msg["uid"]);
    var playerService = this.app.get("playerService");
    playerService.findByUid(uid, function (player) {
        if(!!player) {
            callback({
                resultCode: 0,
                message: "ok",
                player: player.toJSON()
            });
        }
        else {
            callback({
                resultCode: 404,
                message: "玩家信息为空"
            });
        }
    });
};

Remote.prototype.getPlayerInfoByUnionid = function (msg, callback) {
    logger.info("get playerInfo msg = " + JSON.stringify(msg));
    if (!msg.hasOwnProperty("unionid")) {
        callback({
            resultCode: 404,
            message: "unionid为空"
        });
        return;
    }
    var unionid = msg["unionid"];
    var playerService = this.app.get("playerService");
    playerService.findByUnionid(unionid, function (result) {
        if(!!result) {
            callback({
                resultCode: 0,
                message: "ok",
                result: result
            });
        }
        else {
            callback({
                resultCode: 404,
                message: "玩家信息为空"
            });
        }
    });
};

Remote.prototype.getPlayerInfoByNickname = function (msg, callback) {
    logger.info("get playerInfo msg = " + JSON.stringify(msg));
    if (!msg.hasOwnProperty("nickname")) {
        callback({
            resultCode: 404,
            message: "nickname为空"
        });
        return;
    }
    var nickname = msg["nickname"];
    var playerService = this.app.get("playerService");
    playerService.findByNickname(nickname, function (player) {
        if(!!player) {
            callback({
                resultCode: 0,
                message: "ok",
                player: player.toJSON()
            });
        }
        else {
            callback({
                resultCode: 404,
                message: "玩家信息为空"
            });
        }
    });
};

Remote.prototype.getOnlinePlayersInfo = function (msg, callback) {
    logger.info("get all online players info msg = " + JSON.stringify(msg));
    var playerService = this.app.get("playerService");

    var onlinePlayers = playerService.findAllOnlinePlayers();
    if(!!onlinePlayers) {
        callback({
            resultCode: 0,
            message: "ok",
            players: onlinePlayers
        });
    }
    else {
        callback({
            resultCode: 404,
            message: "玩家信息为空"
        });
    }
};

Remote.prototype.getAllDisablePlayersInfo = function (msg, callback) {
    logger.info("get all disable players info msg = " + JSON.stringify(msg));
    var playerService = this.app.get("playerService");

    playerService.getAllDisablePlayers(function (disablePlayers) {
        if(!!disablePlayers) {
            callback({
                resultCode: 0,
                message: "ok",
                players: disablePlayers
            });
        }
        else {
            callback({
                resultCode: 404,
                message: "disablePlayers信息为空"
            });
        }
    });
};

Remote.prototype.getPlayerRegisterInfos = function (msg, callback) {
    logger.info("getPlayerRegisterInfos msg = " + JSON.stringify(msg));
    if (!msg.hasOwnProperty("uids")) {
        callback({
            resultCode: 404,
            message: "uids为空"
        });
        return;
    }
    let suids = msg["uids"].split(",");
    let uids = [];
    for(let uid of suids) {
        uids.push(parseInt(uid));
    }

    let startTime = 0;
    let endTime = 32503654860000;
    if (msg.hasOwnProperty("startTime")) {
        startTime = parseInt(msg["startTime"]);
    }
    if (msg.hasOwnProperty("endTime")) {
        endTime = parseInt(msg["endTime"]);
    }

    let playerService = this.app.get("playerService");
    playerService.getPlayerRegisterInfos(uids, startTime, endTime, function (players) {
        logger.info(`getPlayerRegisterInfos ${JSON.stringify(players)} `);
        if(!!players) {
            let serializePlayers = [];
            for(let player of players) {
                serializePlayers.push({
                    uid: player.accountComponent.uid,
                    nickname: player.accountComponent.nickname,
                    createTime: player.accountComponent.createTime
                });
            }
            callback({
                resultCode: 0,
                message: "ok",
                players: serializePlayers
            });
        }
        else {
            callback({
                resultCode: 404,
                message: "players信息为空"
            });
        }
    });
};
